<?php
namespace Admin\Controller;
use Think\Image;

use Think\Page;

use Think\Controller;
use Common\Controller\AuthController;
class ScenicTicketController extends AuthController {
	public function index(){
		$ticket = D("ScenicTicket");

		$count = $ticket->count();
		$page = new Page($count,4);
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$list = $ticket->relation(true)->limit($page->firstRow,$page->listRows)->select();

		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$file = $_FILES['image'];
			$uploadpath = "Public/Upload/Ticket/";//上传路径

			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");

			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->save($uploadpath.$fileName);

			$data = $_POST;
			$ticket = D("ScenicTicket");
			$data["image"] = $uploadpath.$fileName;
			$data = $ticket->create($data);
			$result= $ticket->add($data);
			if($result){
				$this->success("添加成功","index");
			}else{
				$this->error("添加失败");
			}
		}else{
			$scenic = D('Scenic');
			$list = $scenic->select();
			$this->assign("list",$list);
			$this->display();
		}
	}
	public function edit(){
		$id = I('tid');
		$ticket = M('scenic_ticket')->where(array('id'=>$id))->find();
		$this->assign('ticket',$ticket);
		$this->display();
	}
	
	public function change(){
	
		$file = $_FILES['newimg'];
		file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
		file_put_contents("Public/Log/log.txt", "file:".json_encode($_FILES)."\r\n",FILE_APPEND|LOCK_EX);
		if($file['size']>0){
			$where['name'] = I('ticketname');
			$image = M('scenic_ticket')->where($where)->getField('image');
			$vv   = substr($image, 25);
			if(is_file($vv)){
				unlink($vv);
			}
			$uploadpath = "Public/Upload/Ticket/";//上传路径
	
			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建
	
			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");
	
			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->save($uploadpath.$fileName);
	
			$data["image"] = "http://qianmosen.cn/tubu/".$uploadpath.$fileName;
		}
		$data['name'] = I('ticketname');
		$data['price'] = I('price');
		$data['original'] = I('original');
		if(M('scenic_ticket')->where(array('name'=>$data['name']))->save($data)){
			$this->ajaxReturn(array('status'=>'1','imgurl'=>$data["image"]));
		}
	
	}
	
	public function delete(){
		$where['id'] = $_GET['id'];
		$ticket = D('ScenicTicket');
		$list = $ticket->where($where)->find();
		if(is_file($list['image'])){
			unlink($list['image']);
		}
		$result = $ticket->where($where)->delete();
		if($result){
			$this->success("删除成功","index");
		}else{
			$this->error("删除失败");
		}
	}
}